package com.opmsys.mapper;

import com.opmsys.pojo.entity.Maintain;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface MaintainMapper {

    /**
     * 新增一条保养记录
     * @param estimated_date
     * @param real_date
     * @param maintain_items
     * @param maintain_details
     * @param person_number
     * @param pump_id
     * @return
     */
    @Insert("insert into t_maintain(estimated_date,real_date,maintain_items,maintain_details,person_number,pump_id)values(#{estimated_date},#{real_date},#{maintain_items},#{maintain_details},#{person_number},#{pump_id})")
    public int addMaintain(@Param("estimated_date") String estimated_date, @Param("real_date")String real_date, @Param("maintain_items")String maintain_items, @Param("maintain_details")String maintain_details, @Param("person_number")Long person_number, @Param("pump_id")Integer pump_id);

    /**
     * 根据id更新保养信息
     * @param id
     * @param estimatedDate
     * @param realDate
     * @param maintainItems
     * @param maintainDetails
     * @param personNumber
     * @param pumpId
     * @return
     */
    @Update("<script>" +
            "UPDATE t_maintain " +
            "<set>" +
            "<if test='estimatedDate != null and estimatedDate != \"\" '> estimated_date = #{estimatedDate}, </if>" +
            "<if test='realDate != null and realDate != \"\" '> real_date = #{realDate}, </if>" +
            "<if test='maintainItems != null and maintainItems != \"\" '> maintain_items = #{maintainItems}, </if>" +
            "<if test='maintainDetails != null and maintainDetails != \"\" '> maintain_details = #{maintainDetails}, </if>" +
            "<if test='personNumber != null'> person_number = #{personNumber}, </if>" +
            "<if test='pumpId != null'> pump_id = #{pumpId}, </if>" +
            "</set> " +
            "<where>" +
            "<if test='id != null'> id = #{id} </if>" +
            "</where> " +
            "</script>")
    public int update(@Param("id")Integer id,@Param("estimatedDate") String estimatedDate, @Param("realDate")String realDate, @Param("maintainItems")String maintainItems, @Param("maintainDetails")String maintainDetails, @Param("personNumber")Long personNumber, @Param("pumpId")Integer pumpId);

    /**
     * 按条件分页查询保养记录
     * @param id
     * @param estimatedDate
     * @param realDate
     * @param maintainItems
     * @param maintainDetails
     * @param personNumber
     * @param pumpId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Select("<script>" +
            "SELECT * FROM t_maintain " +
            "<where>" +
            "<if test='id != null'> AND id = #{id} </if>" +
            "<if test='maintainItems != null'> AND maintain_items like concat('%',#{maintainItems},'%')  </if>" +
            "<if test='personNumber != null'> AND person_number = #{personNumber}  </if>" +
            "<if test='pumpId != null'> AND pump_id = #{pumpId}  </if>" +
            "<if test='maintainDetails != null'> AND  maintain_details like concat('%',#{maintainDetails},'%')  </if>" +
            "<if test='realDate != null'> AND real_date like concat('%',#{realDate},'%')  </if>" +
            "<if test='estimatedDate != null'> AND estimated_date like concat('%',#{estimatedDate},'%')  </if>" +
            "</where> " +
            "ORDER BY id " +
            "LIMIT #{pageNum}, #{pageSize}"+
            "</script>")
    public List<Maintain> select(@Param("id")Integer id,
                                     @Param("estimatedDate") String estimatedDate,
                                     @Param("realDate")String realDate,
                                     @Param("maintainItems")String maintainItems,
                                     @Param("maintainDetails")String maintainDetails,
                                     @Param("personNumber")Long personNumber,
                                     @Param("pumpId")Integer pumpId,
                                     @Param("pageNum")Integer pageNum,
                                     @Param("pageSize")Integer pageSize
        );

    /**
     * 根据id删除一条保养记录
     * @param id
     * @return
     */
    @Delete("DELETE FROM t_maintain WHERE id = #{id}")
    public int delete(@Param("id") Integer id);

    /**
     * 根据条件查询全部保养记录
     * @param id
     * @param estimatedDate
     * @param realDate
     * @param maintainItems
     * @param maintainDetails
     * @param personNumber
     * @param pumpId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Select("<script>" +
            "SELECT * FROM t_maintain " +
            "<where>" +
            "<if test='id != null'> AND id = #{id} </if>" +
            "<if test='maintainItems != null'> AND maintain_items like concat('%',#{maintainItems},'%')  </if>" +
            "<if test='personNumber != null'> AND person_number = #{personNumber}  </if>" +
            "<if test='pumpId != null'> AND pump_id = #{pumpId}  </if>" +
            "<if test='maintainDetails != null'> AND  maintain_details like concat('%',#{maintainDetails},'%')  </if>" +
            "<if test='realDate != null'> AND real_date like concat('%',#{realDate},'%')  </if>" +
            "<if test='estimatedDate != null'> AND estimated_date like concat('%',#{estimatedDate},'%')  </if>" +
            "</where> " +
            "ORDER BY id " +
            "</script>")
    public List<Maintain> selectAll(@Param("id")Integer id,
                                 @Param("estimatedDate") String estimatedDate,
                                 @Param("realDate")String realDate,
                                 @Param("maintainItems")String maintainItems,
                                 @Param("maintainDetails")String maintainDetails,
                                 @Param("personNumber")Long personNumber,
                                 @Param("pumpId")Integer pumpId,
                                 @Param("pageNum")Integer pageNum,
                                 @Param("pageSize")Integer pageSize
    );
}
